/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poli.reserva.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import poli.reserva.base.reservaSilla;
import poli.reserva.base.vuelo;

/**
 *
 * @author Pc
 */
public class JReservaSillaMySQL {
    
    
    
    public List<vuelo> selectJReservaSillaMySQL(){
        
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
        ArrayList resultado = new ArrayList();
        
    
        try {
        
            
         String query = "SELECT * FROM reserva_silla ;";   
        
        st = conn.createStatement();   
        
         rs = st.executeQuery(query);   
           
            while (rs.next())
      {
           reservaSilla reservaSilla = new reservaSilla();
           reservaSilla.setId_reserva(rs.getString(1));
           reservaSilla.setId_vuelo(rs.getString(2));
           reservaSilla.setNum_silla(rs.getString(3));
           reservaSilla.setLetra_silla(rs.getString(4));
           reservaSilla.setIdusuario(rs.getString(5));
           reservaSilla.setFec_reserva(rs.getDate(6));
          
           
           resultado.add(reservaSilla);
                }
            
            st.close();
           }
           
       
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        return resultado;
        
    }
    public List<vuelo> selectJReservaSillaMySQLiD_reserva(String id_reserva_silla){
        
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
        ArrayList resultado = new ArrayList();
        
    
        try {
        
            
         String query = "SELECT * FROM reserva_silla where id_reserva = '"+id_reserva_silla+"';";   
        
        st = conn.createStatement();   
        
         rs = st.executeQuery(query);   
           
            while (rs.next())
      {
           reservaSilla reservaSilla = new reservaSilla();
           reservaSilla.setId_reserva(rs.getString(1));
           reservaSilla.setId_vuelo(rs.getString(2));
           reservaSilla.setNum_silla(rs.getString(3));
           reservaSilla.setLetra_silla(rs.getString(4));
           reservaSilla.setIdusuario(rs.getString(5));
           reservaSilla.setFec_reserva(rs.getDate(6));
          
           
           resultado.add(reservaSilla);
                }
            
            st.close();
           }
           
       
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        return resultado;
        
    }
    
    public Boolean insertJReservaSillaMySQL(reservaSilla JReservaSilla){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
         
         boolean result = false;
          try {
        
            
         String query = "INSERT INTO reserva_silla (id_reserva, id_vuelo, numero_silla, letra_silla, idusuario ,fec_reserva)"+
                 " VALUES ( '"+JReservaSilla.getId_reserva()+"' , '"+JReservaSilla.getId_vuelo()+"' , '"+JReservaSilla.getNum_silla()+"' , '"+JReservaSilla.getLetra_silla()+"' , '"+JReservaSilla.getIdusuario()+"', '"+JReservaSilla.getFec_reserva()+"' );";   
        
        st = conn.createStatement();
        
        result =  st.execute(query);
        
            conn.close();     
            st.close();
           }
           
      
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        
         if (result!= true){
             
             return result;
         }
         
         else
             
        return true;
        
    }
    
     public Boolean deleteJReservaSillaMySQL(String id_reserva){
        
         Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
         
         boolean result = false;
          try {
        
            
       
              String query ="DELETE FROM reserva_silla WHERE id_reserva = ?  ";
        
        PreparedStatement preparedStmt = conn.prepareStatement(query);
        
        
      preparedStmt.setString(1, id_reserva);
      
        
         preparedStmt.executeUpdate();
        
            conn.close();     
            
           }
           
      
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        
         if (result!= false){
             
             return result;
         }
         
         else
             
        return true;
        
    }
     
     public Boolean updateJReservaSillaMySQL(String id_reserva , String idusuario , String letra , String numero , String estado_silla){
        
         Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conexion conect = new conexion();
         conn=  conect.conexionMySQL();
         
         boolean result = false;
          try {
        
            
      
              String query ="UPDATE reserva_silla SET estado_reservacion_silla =  ? WHERE numero_silla = ?  and letra_silla = ? and idusaurio =? ";
        
        PreparedStatement preparedStmt = conn.prepareStatement(query);
        
        
      preparedStmt.setString(1, estado_silla);
      preparedStmt.setString(2, numero);
      preparedStmt.setString(3, letra);
      preparedStmt.setString(4, idusuario);
        
         preparedStmt.executeUpdate();
        
            conn.close();     
            
           }
           
      
    catch (SQLException ex) {
           System.err.println(ex.getMessage());
        }
        
         if (result!= false){
             
             return result;
         }
         
         else
             
        return true;
               
        
    }
    
}
